
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<style type="text/css" media="screen">
    .calendar_th{
        cursor: pointer;
    }
    .calendar_active{
        background: #337ab7;
        color:#fff;
    }
</style>
<script type="text/javascript">
    var dates=[];
    $(function(){
        init('workPlanList_t1');
    })
    function init(element){
        var d=new Date();
        var year=d.getFullYear();
        var month=d.getMonth()+1;
        var day=d.getDate();
        getByMonthforworkPlan(year+"-"+month);
        $('#workPlan_selectDate_workPlan').html(year+"-"+month+"-"+day);
        getBySaveDateforworkPlan(year+"-"+month+"-"+day);
        renderCalendar_workPlan(year,month,element);
    }

    function getBySaveDateforworkPlan(saveDate){
        var userid='<shiro:principal property="id"/>';
        $('#workPlanList2_ul').html('');
        $.ajax({
            url:'${ctxPath}/workPlan/getBySaveDate',
            type:'post',
            data:{saveDate:saveDate,userid:userid},
            dataType:'json',
            success:function(result){
                var html="";
                if(!isNull(result)){
                    for(var i=0;i<result.length;i++){
                        html+="<li><b>"+result[i].name+"</b><br>"+result[i].content;
                        if(result[i].attachList.length>0){
                            html+="<br>";
                            for(var k=0;k<result[i].attachList.length;k++){
                                var attach=result[i].attachList[k];
                                html+=attach.name;
                                if(attach.type=='pdf'){
                                    html+="<a style='margin-left: 20px;'"
                                        +"href='/jsp/pdfjs/web/viewer.html?file=/pdfView/view?name%3d"+attach.id+"'"
                                        +"target='_blank'>在线预览</a>";
                                }
                                html+=" <a style='margin-left: 20px;' href='javascript:void(0)'"
                                    +"onClick='downloadAttach(\""+attach.id+"\")'>下载</a>";
                            }
                        }
                        if(result[i].workCommentOpinionList.length>0){
                            for(var j=0;j<result[i].workCommentOpinionList.length;j++){
                                if(!isNull(result[i].workCommentOpinionList[j].commentTime)) {
                                    html += "<ul><li>评论人：" + result[i].workCommentOpinionList[j].commentatorName + "  &nbsp;&nbsp;&nbsp;" + datetime2Str(result[i].workCommentOpinionList[j].commentTime) +
                                        "<br>" + result[i].workCommentOpinionList[j].opinion + "</li></ul>"
                                }
                            }
                        }
                        html +="</li>";
                    }
                    $('#workPlanList2_ul').append(html);
                }
            }
        })
    }

    /**
     * 通过年月查询有记录的日期
     */
    function getByMonthforworkPlan(param){
        var userid='<shiro:principal property="id"/>';
        dates=[];
        $.ajax({
            async:false,
            url:'${ctxPath}/workPlan/getByMonth',
            type:'post',
            data:{param:param,userid:userid},
            dataType:'json',
            success:function(result){
                if(result.success){
                    dates=result.obj;
                }else{
                    $.messager.alert('提示',result.msg,'error');
                }
            }
        })
    }

    function renderCalendar_workPlan(year,month,element){
        $('#'+element).html('<div>'
            +'<div style="float:left;margin-bottom:5px;">'
            +'<button class="prev_month" onclick="prevMonth_workPlan(\''+element+'\')">上一个月</button>'
            +'<button class="nex_month " onclick="nextMonth_workPlan(\''+element+'\')">下一个月</button>'
            +'</div>'
            +'<div class="year_month" style="text-align: center;"></div>'
            +'</div>'
            +'<table class="day_cc_calendar" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;clear:' +'both;"></table>');
        $('#'+element+' .year_month').html(year+"年"+month+"月");
        var now=new Date();
        var y=now.getFullYear();
        var m=now.getMonth()+1;
        var d=now.getDate();
        $('#'+element+' .day_cc_calendar').html('');
        var html1="<tr class='calendar_1'>";
        var html2="<tr class='calendar_2'>";
        var days=getDays(year,month);
        for(var i=1;i<=days;i++){
            var circleHtml="";
            var dd;
            if(month<10){
                if(i<10){
                    dd=year+"-0"+month+"-0"+i;
                }else{
                    dd=year+"-0"+month+"-"+i;
                }
            }else{
                if(i<10){
                    dd=year+"-"+month+"-0"+i;
                }else{
                    dd=year+"-"+month+"-"+i;
                }
            }

            if(isInArray(dates,dd)){
                circleHtml="<br/><span style='color:green'>●</span>";
            }
            var week=getWeek_workPlan(Number(year),Number(month),Number(i));
            if(year==y && m==month && d==i){
                html1+="<td style='padding:10px;background:pink;color:#fff;text-align:center'>"+week+"</td>";
                html2+="<td style='padding:5px;background:pink;color:#fff;text-align:center' class='calendar_th' onclick='selectDate_workPlan(\""+element+"\","+year+","+month+","+i+",this)'>"+i+circleHtml+"</td>";
            }else{
                html1+="<td style='padding:10px;text-align:center'>"+week+"</td>";
                html2+="<td style='padding:5px;text-align:center' class='calendar_th' onclick='selectDate_workPlan(\""+element+"\","+year+","+month+","+i+",this)'>"+i+circleHtml+"</td>";
            }
        }
        html1+="</tr>";
        html2+="</tr>";
        $('#'+element+' .day_cc_calendar').append(html1);
        $('#'+element+' .day_cc_calendar').append(html2);
    }
    /**
     * [nextMonth_workPlan 下一个月]
     * @return {[type]} [description]
     */
    function nextMonth_workPlan(element){
        var dstr=$('#'+element+" .year_month").text();
        $('#workPlan_selectDate_workPlan').html("");
        var year=dstr.substr(0,dstr.indexOf("年"));
        var month=dstr.substr(dstr.indexOf("年")+1,dstr.length).replace('月','');
        if(month==12){
            month=1;
            year=Number(year)+1;
        }else{
            month=Number(month)+1;
        }
        $('#'+element+' .year_month').html(year+"年"+month+"月");
        getByMonthforworkPlan(year+"-"+month);
        $('#workPlanList2_ul').html('');
        renderCalendar_workPlan(year,month,element);
    }
    /**
     * [prevMonth_workPlan 上一个月]
     * @return {[type]} [description]
     */
    function prevMonth_workPlan(element){
        var dstr=$('#'+element+" .year_month").text();
        $('#workPlan_selectDate_workPlan').html("");
        var year=dstr.substr(0,dstr.indexOf("年"));
        var month=dstr.substr(dstr.indexOf("年")+1,dstr.length).replace('月','');
        if(month==1){
            month=12;
            year=Number(year)-1;
        }else{
            month=Number(month)-1;
        }
        $('#'+element+' .year_month').html(year+"年"+month+"月");
        getByMonthforworkPlan(year+"-"+month);
        $('#workPlanList2_ul').html('');
        renderCalendar_workPlan(year,month,element);
    }
    /**
     * [getDays 获取某个月有多少天]
     * @param  {[type]} year  [description]
     * @param  {[type]} month [description]
     * @return {[type]}       [description]
     */
    function getDays(year,month){
        //1)是否是闰年
        var b=isLeapYear(year);
        if(b){
            if(month==2){
                return 29;
            }
        }else{
            if(month==2){
                return 28;
            }
        }
        if((month==1) || (month==3) || (month==5) || (month==7) || (month==8) || (month==10) || (month==12)){
            return 31;
        }
        return 30;
    }


    /**
     * 获取星期几
     * @param  {[type]} year  [description]
     * @param  {[type]} month [description]
     * @param  {[type]} day   [description]
     * @return {[type]}       [description]
     */
    function getWeek_workPlan(year,month,day){
        var week = "日一二三四五六".split("")[new Date(Date.UTC(year, month-1, day)).getDay()];
        return week;
    }
    /**
     * 是否是闰年
     * @param  {[type]}  year [description]
     * @return {Boolean}      [description]
     */
    function isLeapYear(year) {
        return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
    }


    function selectDate_workPlan(element,year,month,day,obj){
        getBySaveDateforworkPlan(year+"-"+month+"-"+day);
        $('#workPlan_selectDate_workPlan').html(year+"-"+month+"-"+day);
        $('#'+element+' .day_cc_calendar').find('.calendar_active').removeClass('calendar_active');
        $(obj).addClass('calendar_active');
        var indexA;
        $.each($('#'+element+' .day_cc_calendar .calendar_2 td'), function(index, val) {
            if(obj==val){
                indexA=index+1;
            }
        });
        $.each($('#'+element+' .day_cc_calendar .calendar_1 td'), function(index, val) {
            if(indexA==(index+1)){
                $(val).addClass('calendar_active');
            }
        });
    }
</script>
<div class="easyui-layout" data-options="fit:true,border:false" >
    <div data-options="region:'center',border:false" style="overflow-x:hidden;overflow-y: auto;padding: 3px;" >
        <div id="workPlanList_t1"></div>
        <div id="workPlan_selectDate_workPlan" style="margin-top: 15px;margin-left: 25px;"></div>
        <ul id="workPlanList2_ul">
        </ul>
    </div>

</div>